package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/spin/api")
@AllArgsConstructor
public class SpinController {

    private final HttpServletRequest httpServletRequest;
    private final SpinService spinService;
    private final SpinDetailService spinDetailService;
    private final SpinCarbonateService spinCarbonateService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(Spin spin) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(spin.getNcv())) {
                ncvs = Arrays.asList(spin.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(spin.getFc())) {
                fcs = Arrays.asList(spin.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(spin.getCc())) {
                ccs = Arrays.asList(spin.getCc().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(spin.getOf())) {
                ofs = Arrays.asList(spin.getOf().split(","));
            }
            List<String> fCarbonates = new ArrayList<>();
            if (StringUtils.isNotBlank(spin.getFCarbonates())) {
                fCarbonates = Arrays.asList(spin.getFCarbonates().split(","));
            }
            List<String> fis = new ArrayList<>();
            if (StringUtils.isNotBlank(spin.getFis())) {
                fis = Arrays.asList(spin.getFis().split(","));
            }
            List<String> mCarbonates = new ArrayList<>();
            if (StringUtils.isNotBlank(spin.getMCarbonates())) {
                mCarbonates = Arrays.asList(spin.getMCarbonates().split(","));
            }

            Double eBurn = SpinUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double eProcess = SpinUtil.getEProcess(fCarbonates, fis, mCarbonates);
            Double eWasteWater = SpinUtil.getEWasteWater(spin.getW(), spin.getCodin(), spin.getCodout(), spin.getR());
            Double ePurchaseElectric = SpinUtil.getEPurchaseElectric(spin.getAdPurchaseElectric(), spin.getEfPurchaseElectric());
            Double ePurchaseHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(spin.getAdPurchaseHeat());
            Double eOutputHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(spin.getAdOutInputHeat());
            Double eOutputElectric = SpinUtil.getEOutputElectric(spin.getAdOutInputElectric(), spin.getEfOutInputElectric());
            Double result = SpinUtil.getE(eBurn, eProcess, eWasteWater, ePurchaseElectric,
                    ePurchaseHeat, eOutputElectric, eOutputHeat);
            spin.setCustomerId(customer.getId());
            spin.setCreateTime(new Date());
            spin.setEWasteWater(eWasteWater);
            spin.setEBurn(GasDCUtil.round(eBurn, 2));
            spin.setEProcess(GasDCUtil.round(eProcess, 2));
            spin.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            spin.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            spin.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            spin.setEOutputElectric(GasDCUtil.round(eOutputElectric, 2));
            spin.setState(Constants.STATE_INVAILD);
            spin.setResult(GasDCUtil.getThousandth(result));
            spinService.save(spin);
            for (int i = 0; i < ncvs.size(); i++) {
                SpinDetail spinDetail = new SpinDetail();
                spinDetail.setNcv(Double.valueOf(ncvs.get(i)));
                spinDetail.setFc(Double.valueOf(fcs.get(i)));
                spinDetail.setCc(Double.valueOf(ccs.get(i)));
                spinDetail.setOf(Double.valueOf(ofs.get(i)));
                spinDetail.setSpinId(spin.getId());
                spinDetailService.save(spinDetail);
            }
            for (int i = 0; i < fCarbonates.size(); i++) {
                SpinCarbonate spinCarbonate = new SpinCarbonate();
                spinCarbonate.setFCarbonate(Double.valueOf(fCarbonates.get(i)));
                spinCarbonate.setMCarbonate(Double.valueOf(mCarbonates.get(i)));
                spinCarbonate.setFi(Double.valueOf(fis.get(i)));
                spinCarbonate.setSpinId(spin.getId());
                spinCarbonateService.save(spinCarbonate);
            }
            return ResultUtil.success("计算成功", spin);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
